**Replication File for Conflict Environment Scores
*** Reid, Myrick, Kadera, & Crescenzi (2020, JOGSS) ***

*This do-file contains replication details for the Conflict Environment (CE) Scores in RMKC 2020 (JOGSS).
*See "Civil War RMKC Rep Do File" for replication of the figures and tables in the paper and appendix. 


*** DO-FILE FOR CONFLICT ENVIRONMENT SCORES ****

* Spatial Lag generated using C-Shapes Package in R from Weidmann, Kuse, and Gleditsch (2010)
* Temporal Lag adopted from do-file of Crescenzi & Enterline (2001)

************************************************************************
** Common Abbreviations: 
** slc = spatial lag of conflict
** slco = spatial lag of conflict onset
** cces = civil conflict environment score
** ices = interstate conflict environment score
** inter = interstate war
** civil = civil war
** cow = COW Wars
** acd = UDCP/PRIO Armed Conflict Dataset Wars version 4
** acd3 = UDCP/PRIO Armed Conflict Data version 3 
** fl = Fearon & Laitin Wars (2003) 
** s = Sambanis Wars (2004) 

************************************************************************

** I. BASELINE MODEL USED FOR GENERATING CONFLICT ENVIRONMENT SCORES (CES SCORES)

** Example used: COW Intrastate War (v. 4.1) Data
** Generate relevant spatial lag variables in R before using this Do-File
** Use "Base Data for Spatial Lags of Conflict.dta" with this do-file 

* Generate Variable for the Existence of Spatially Lagged Intrastate Conflict

sort ccode year
qui by ccode: gen exist_slc = .
replace exist_slc = 0 if slc_civil == 0 
replace exist_slc = 1 if slc_civil > 0 & slc_civil~=.

* Generate Time Passed Variable for Years Separating Conflict Events

qui by ccode: gen lexist_slc = exist_slc[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slc = 1
qui by ccode: replace yrs_slc= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slc~=.
qui by ccode: replace yrs_slc= (yrs_slc[_n-1] + (year - lyear)) if yrs_slc[_n-1]~=. & lyear~=. & lexist_slc~=. & exist_slc==0
qui by ccode: replace yrs_slc =. if slc_civil==. & slco_civil==. 

* Generate Variable for the Existence of Spatially Lagged Intrastate Conflict Onset 

qui by ccode: gen exist_slco_civil = .
replace exist_slco_civil = 0 if slco_civil == 0 
replace exist_slco_civil = 1 if slco_civil > 0 & slco_civil~=.

* Generate Intrastate Conflict Environment Score

gen double cces_cow=0 
qui by ccode: replace cces_cow = slco_civil if exist_slco_civil==1 & slco_civil~=. & cces_cow[_n-1]==.
qui by ccode: replace cces_cow= slco_civil if exist_slco_civil==1 & cces_cow[_n-1]==0.
qui by ccode: replace cces_cow = (exp(-(1+yrs_slc[_n-1])/(1+cowruntot_1))*(cces_cow[_n-1])) +  slco_civil if slco_civil~=. & cces_cow[_n-1]~=.
qui by ccode: replace cces_cow=. if slco_civil==. & slc_civil==. 
label var cces_cow "Civil CE Score (COW)"

* Drop Extra Variables

drop exist_slc lexist_slc yrs_slc exist_slco_civil lyear


** II. CIVIL/ INTRASTATE CONFLICT ENVIRONMENT SCORES (CCES)

*COW: exist_slc / slc_civil / slco_civil (USED IN ANALYSES)
*ACDv4.1: exist_slca / history_slca / slc_acd / slco_acd (USED IN ANALYSES)
*ACDv3: exist_slca3 / slc_acd3 / slco_acd3
*F&L 2003: exist_slcfl / slc_fl / slco_fl
*Sambanis 2004: exist_slcs / slc_s / slco_s

** CCES COW 

sort ccode year
qui by ccode: gen exist_slc = .
replace exist_slc = 0 if slc_civil == 0 
replace exist_slc = 1 if slc_civil > 0 & slc_civil~=.
qui by ccode: gen lexist_slc = exist_slc[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slc = 1
qui by ccode: replace yrs_slc= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slc~=.
qui by ccode: replace yrs_slc= (yrs_slc[_n-1] + (year - lyear)) if yrs_slc[_n-1]~=. & lyear~=. & lexist_slc~=. & exist_slc==0
qui by ccode: replace yrs_slc =. if slc_civil==. & slco_civil==. 
qui by ccode: gen exist_slco_civil = .
replace exist_slco_civil = 0 if slco_civil == 0 
replace exist_slco_civil = 1 if slco_civil > 0 & slco_civil~=.
gen double cces_cow=0 
qui by ccode: replace cces_cow = slco_civil if exist_slco_civil==1 & slco_civil~=. & cces_cow[_n-1]==.
qui by ccode: replace cces_cow= slco_civil if exist_slco_civil==1 & cces_cow[_n-1]==0.
qui by ccode: replace cces_cow = (exp(-(1+yrs_slc[_n-1])/(1+cowruntot_1))*(cces_cow[_n-1])) +  slco_civil if slco_civil~=. & cces_cow[_n-1]~=.
qui by ccode: replace cces_cow=. if slco_civil==. & slc_civil==. 
label var cces_cow "Civil CE Score (COW)"
qui by ccode: gen cces_cow_1 = cces_cow[_n-1]
label var cces_cow_1 "Civil CE Score (COW), lagged"
drop exist_slc lexist_slc yrs_slc exist_slco_civil lyear

** CCES ACD

sort ccode year
qui by ccode: gen exist_slca = .
replace exist_slca = 0 if slc_acd == 0 
replace exist_slca = 1 if slc_acd > 0 & slc_acd~=.
qui by ccode: gen lexist_slca = exist_slca[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slca = 1
qui by ccode: replace yrs_slca= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slca~=.
qui by ccode: replace yrs_slca= (yrs_slca[_n-1] + (year - lyear)) if yrs_slca[_n-1]~=. & lyear~=. & lexist_slca~=. & exist_slca==0
qui by ccode: replace yrs_slca =. if slc_acd==. & slco_acd==. 
qui by ccode: gen exist_slco_acd = .
replace exist_slco_acd = 0 if slco_acd == 0 
replace exist_slco_acd = 1 if slco_acd > 0 & slco_acd~=.
gen double cces_acd=0 
qui by ccode: replace cces_acd= slco_acd if exist_slco_acd==1 & slco_acd~=. & cces_acd[_n-1]==.
qui by ccode: replace cces_acd= slco_acd if exist_slco_acd==1 & cces_acd[_n-1]==0.
qui by ccode: replace cces_acd = (exp(-(1+yrs_slca[_n-1])/(1+acdruntot_1))*(cces_acd[_n-1])) + slco_acd if slco_acd~=. & cces_acd[_n-1]~=.
qui by ccode: replace cces_acd=. if slco_acd==. & slc_acd==. 
label var cces_acd "Civil CE Score (ACD)"
qui by ccode: gen cces_acd_1 = cces_acd[_n-1]
label var cces_acd_1 "Civil CE Score (ACD), lagged"
drop exist_slca lexist_slca yrs_slca exist_slco_acd lyear

** CCES Samanbis 2004

sort ccode year
qui by ccode: gen exist_slcs = .
replace exist_slcs = 0 if slc_s == 0 
replace exist_slcs = 1 if slc_s > 0 & slc_s~=.
qui by ccode: gen lexist_slcs = exist_slcs[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slcs = 1
qui by ccode: replace yrs_slcs= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slcs~=.
qui by ccode: replace yrs_slcs= (yrs_slcs[_n-1] + (year - lyear)) if yrs_slcs[_n-1]~=. & lyear~=. & lexist_slcs~=. & exist_slcs==0
qui by ccode: replace yrs_slcs =. if slc_s==. & slco_s==. 
qui by ccode: gen exist_slco_s = .
replace exist_slco_s = 0 if slco_s == 0 
replace exist_slco_s = 1 if slco_s > 0 & slco_s~=.
gen double cces_s=0 
qui by ccode: replace cces_s= slco_s if exist_slco_s==1 & slco_s~=. & cces_s[_n-1]==.
qui by ccode: replace cces_s= slco_s if exist_slco_s==1 & cces_s[_n-1]==0.
qui by ccode: replace cces_s = (exp(-(1+yrs_slcs[_n-1])/(1+samruntot_1))*(cces_s[_n-1])) + slco_s if slco_s~=. & cces_s[_n-1]~=.
qui by ccode: replace cces_s=. if slco_s==. & slc_s==. 
label var cces_s "Civil CE Score (Sambanis)"
qui by ccode: gen cces_s_1 = cces_s[_n-1]
label var cces_s_1 "Civil CE Score (Sambanis), lagged"
drop exist_slcs lexist_slcs yrs_slcs exist_slco_s lyear

** CCES F&L 2003

sort ccode year
qui by ccode: gen exist_slcfl = .
replace exist_slcfl = 0 if slc_fl == 0 
replace exist_slcfl = 1 if slc_fl > 0 & slc_fl~=.
qui by ccode: gen lexist_slcfl = exist_slcfl[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slcfl = 1
qui by ccode: replace yrs_slcfl= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slcfl~=.
qui by ccode: replace yrs_slcfl= (yrs_slcfl[_n-1] + (year - lyear)) if yrs_slcfl[_n-1]~=. & lyear~=. & lexist_slcfl~=. & exist_slcfl==0
qui by ccode: replace yrs_slcfl =. if slc_fl==. & slco_fl==. 
qui by ccode: gen exist_slco_fl = .
replace exist_slco_fl = 0 if slco_fl == 0 
replace exist_slco_fl = 1 if slco_fl > 0 & slco_fl~=.
gen double cces_fl=0 
qui by ccode: replace cces_fl= slco_s if exist_slco_fl==1 & slco_fl~=. & cces_fl[_n-1]==.
qui by ccode: replace cces_fl= slco_s if exist_slco_fl==1 & cces_fl[_n-1]==0.
qui by ccode: replace cces_fl = (exp(-(1+yrs_slcfl[_n-1])/(1+flruntot_1))*(cces_fl[_n-1])) + slco_fl if slco_fl~=. & cces_fl[_n-1]~=.
qui by ccode: replace cces_fl=. if slco_fl==. & slc_fl==. 
label var cces_fl "Civil CE Score (F&L)"
qui by ccode: gen cces_fl_1 = cces_fl[_n-1]
label var cces_fl_1 "Civil CE Score (F&L), lagged"
drop exist_slcfl lexist_slcfl yrs_slcfl exist_slco_fl lyear

** CCES ACD v 3

sort ccode year
qui by ccode: gen exist_slca3 = .
replace exist_slca3 = 0 if slc_acd3 == 0 
replace exist_slca3 = 1 if slc_acd3 > 0 & slc_acd3~=.
qui by ccode: gen lexist_slca3 = exist_slca3[_n-1]
qui by ccode: gen lyear = year[_n-1]
gen yrs_slca3 = 1
qui by ccode: replace yrs_slca3= (year - lyear) if (year - lyear) ~= 1 & lyear~=. & lexist_slca3~=.
qui by ccode: replace yrs_slca3= (yrs_slca3[_n-1] + (year - lyear)) if yrs_slca3[_n-1]~=. & lyear~=. & lexist_slca3~=. & exist_slca3==0
qui by ccode: replace yrs_slca3 =. if slc_acd3==. & slco_acd3==. 
qui by ccode: gen exist_slco_acd3 = .
replace exist_slco_acd3 = 0 if slco_acd3 == 0 
replace exist_slco_acd3 = 1 if slco_acd3 > 0 & slco_acd3~=.
gen double cces_acd3=0 
qui by ccode: replace cces_acd3= slco_acd3 if exist_slco_acd3==1 & slco_acd3~=. & cces_acd3[_n-1]==.
qui by ccode: replace cces_acd3= slco_acd3 if exist_slco_acd3==1 & cces_acd3[_n-1]==0.
qui by ccode: replace cces_acd3 = (exp(-(1+yrs_slca3[_n-1])/(1+acd3runtot_1))*(cces_acd3[_n-1])) + slco_acd3 if slco_acd3~=. & cces_acd3[_n-1]~=.
qui by ccode: replace cces_acd3=. if slco_acd3==. & slc_acd3==. 
label var cces_acd3 "Civil CE Score (ACD v3)"
qui by ccode: gen cces_acd3_1 = cces_acd3[_n-1]
label var cces_acd3_1 "Civil CE Score (ACD v3), lagged"
drop exist_slca3 lexist_slca3 yrs_slca3 exist_slco_acd3 lyear

****Extra: In base data file, use the following code to generate running totals for conflicts

 sort ccode year
 qui by ccode: gen cowruntot = sum(cowonset)
 label var cowruntot "Running Total of Conflicts (COW)"
 qui by ccode: gen cowruntot_1 = cowruntot[_n-1]
 label var cowruntot_1 "Running Total of Conflicts (COW), lagged"

 sort ccode year
 qui by ccode: gen acdruntot = sum(acdonset)
 label var acdruntot "Running Total of Conflicts (ACD)"
 qui by ccode: gen acdruntot_1 = acdruntot[_n-1]
  label var acdruntot_1 "Running Total of Conflicts (ACD), lagged"

 sort ccode year
 qui by ccode: gen flruntot = sum(flonset)
 label var flruntot "Running Total of Conflicts (F&L)"
 qui by ccode: gen flruntot_1 = flruntot[_n-1]
 label var flruntot_1 "Running Total of Conflicts (F&L), lagged"

 sort ccode year
 qui by ccode: gen samruntot = sum(samonset)
 label var samruntot "Running Total of Conflicts (Sambanis)"
 qui by ccode: gen samruntot_1 = samruntot[_n-1]
 label var samruntot_1 "Running Total of Conflicts (Sambanis), lagged"
